package cn.jy.controller;

import cn.jy.bean.Msg;
import cn.jy.pojo.Administrators;
import cn.jy.service.AdminService;
import cn.jy.service.AppService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class AppController {

    @Autowired
    private AppService appService;
    @Autowired
    private AdminService adminService;


    @ResponseBody
    @GetMapping("/books")
    public Msg books(@RequestParam(value = "pageNum",defaultValue = "1") int pn){
        if (pn<1){
            pn=1;
        }
        PageHelper.startPage(pn,8);
        List<Administrators> administrators=adminService.Administratorsgly();
        PageInfo<Administrators> administratorsPageInfo = new PageInfo<Administrators>(administrators,100);
        /* PageInfo<administrators> booksPageInfo = new PageInfo<administrators>(administrators, 100);*/
        //将分页信息传入自定义类的返回值（returnData）
        return Msg.success().add("books",administratorsPageInfo);
    }


    @ResponseBody
    @RequestMapping(value = "/dele/{id}/{state}",method = RequestMethod.DELETE)
    public Msg delBook(@PathVariable("id") Integer id,@PathVariable("state") Integer state){
        int i = adminService.updateState(state, id);
        if (i>0){
            return Msg.success().add("msg","success");
        }
        return Msg.fail().add("msg","error");
    }
}
